package com.xinhe.web.controller;

import com.querydsl.core.QueryResults;
import com.xinhe.web.annotation.WebUsed;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.Goods;
import com.xinhe.web.request.GoodsModel;
import com.xinhe.web.response.GoodsView;
import com.xinhe.web.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(value = "产品列表", tags = {"产品列表"})
@RestController
@RequestMapping(value = {"/goods"}, produces = {"application/json;set=UTF-8"})

public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "分页查询商品")
    @RequestMapping({"/pageGoods"})
    public QueryResults<GoodsView> pageGoods(int pageNum, int pageSize) {
        return this.goodsService.pageGoods(pageNum,pageSize);
    }
    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "商品详情")
    @ApiImplicitParam(name = "goodsId", value = "标识", dataTypeClass = Long.class, required = true)
    @RequestMapping({"/detailGoods"})
    public Result<Goods> detailGoods(Long goodsId) {
        return this.goodsService.findById(goodsId);
    }

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "删除商品")
    @ApiImplicitParam(name = "goodsId", value = "标识", dataTypeClass = Long.class, required = true)
    @RequestMapping({"/deleteGoods"})
    public Result deleteGoods(Long goodsId) {
        return this.goodsService.delete(goodsId);
    }

    @WebUsed
    @ApiOperation(httpMethod = "POST", value = "平台保存商品")
    @RequestMapping({"/saveGoods"})
    public Result saveGoods(@Valid @RequestBody GoodsModel model) {
        return this.goodsService.saveGoods(model);
    }


    @ApiOperation(httpMethod = "GET", value = "分页查询商家商品")
    @RequestMapping({"/pageGoodsBySeller"})
    public QueryResults<GoodsView> pageGoodsBySeller(Long sellerUserId,int pageNum, int pageSize) {
        return this.goodsService.pageGoodsBySeller(sellerUserId,pageNum,pageSize);
    }

    @ApiOperation(httpMethod = "POST", value = "商家保存商品")
    @PostMapping({"/saveSellerGoods"})
    public Result saveSellerGoods(@RequestBody @Validated GoodsModel model) {
        return this.goodsService.saveSellerGoods(model);
    }


    @ApiOperation(httpMethod = "GET", value = "用户商品收藏列表")
    @RequestMapping({"/findByUserCollect"})
    public Result findByUserCollect() {
        return this.goodsService.findByUserCollect();
    }
}
